<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>复选框</title>
</head>
<body>
    <table border="1" align="center" width="30%">
        <tr>
            <td><input type="checkbox" onclick="fun1(this)"/></td>
            <td>用户名</td>
            <td>密码</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="ids" value="1"/></td>
            <td>张三</td>
            <td>666</td>
        </tr>
        <tr>
            <td><input type="checkbox" name="ids" value="2"/></td>
            <td>李四</td>
            <td>777</td>
        </tr>
        <tr>
            <td><a href="javaScript:fun2();">反选</a></td>
            <td><a href="javaScript:fun3();">删除</a></td>
        </tr>
    </table>
</body>
<script>
    //复选框全选全不选(预先有无选中checked)
    function fun1(obj) {
        let arr = document.getElementsByName("ids");
        for (let i = 0; i < arr.length; i++) {
            // 让每个复选框状态与obj一样
            arr[i].checked=obj.checked;
        }
    }
    //反选(对当前checked状态取反)
    function fun2() {
        let arr = document.getElementsByName("ids");
        for (let i = 0; i < arr.length; i++) {
            arr[i].checked=!arr[i].checked
        }
    }
    //删除
    /*function fun3() {
        let arr = document.getElementsByName("ids");
        for (let i = arr.length-1; i = 0; i--) {
            if(arr[i].checked){
                let val = arr[i].value;

            }
        }
    }*/
    function fun3() {
        let arr = document.getElementsByName("ids");
        let arr1 =new Array();
        for (let i = 0; i < arr.length; i++) {
            if(arr[i].checked){
                let val = arr[i].value;
                arr1.push(arr[i].value)
            }
        }
        location.href="user/delete?ids"+arr1;
    }
</script>
</html>